/*
Add Binary
Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

*/

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include "print.h"
using namespace std;


void testForStack()
{
	stack<int> mystack;
	mystack.push(10);
	mystack.push(20);
	mystack.top() -= 5;
	cout << "mystack.top() is now " << mystack.top() << endl;
}

void testForIntToString()
{
	int a = 10;
	stringstream ss;
	ss << a;
	string str = ss.str();
	cout << str << endl;

	string str1 = to_string(a);

}



class Solution {
public:
	string addBinary(string a, string b) {
		string results = "";
		map<char, int> binaryTalble;
		binaryTalble['1'] = 1;
		binaryTalble['0'] = 0;
	
		int intTemp = 0;
		
		for (int i = a.size() - 1, j = b.size() - 1; i >= 0 || j >= 0; i = i >= 0 ? i-1 : -1, j = j >= 0 ? j-1 : -1)
		{
			if (i != -1 && j != -1)
			{
				intTemp = intTemp + binaryTalble[a[i]] + binaryTalble[b[j]];
				results = to_string(intTemp % 2) + results ;
				intTemp = intTemp / 2;
			}
			else if (i == -1)
			{
				intTemp = intTemp  + binaryTalble[b[j]];
				results = to_string(intTemp % 2) + results;
				intTemp = intTemp / 2;
			}
			else if (j == -1)
			{
				intTemp = intTemp + binaryTalble[a[i]];
				results = to_string(intTemp % 2) + results;
				intTemp = intTemp / 2;
			}
			

		}
		if (intTemp > 0)
		{
			results = to_string(intTemp) + results;
		}

		return results;
	}
};

/*

for (int i = a.size() - 1, j = b.size() - 1; i >= 0 && j >= 0; i = i >= 0 ? --i : -1, j = j >= 0 ? --j : -1)
{
if (i != -1 || j != -1)
{
intTemp = intTemp + binaryTalble[a[i]] + binaryTalble[b[j]];
results = to_string(intTemp % 2) + results ;
intTemp = intTemp / 2;
}
else if (i == -1)
{

}
else if (j == -1)
{

}

}
*/


int main(int argc, char* argv[])
{

	testForSubString();
//	testForIntToString();
	Solution s;
	//string str1, str2;
	vector<string> strVec;
	for (int i = 1; i < argc; i++){

		cout << argv[i] << endl;
		//arrInt[i - 1] = atoi(argv[i]);
		//str = argv[i];
		//charPtr = argv[i];
		//fileName = argv[i];
		strVec.push_back(argv[i]);

	}

	string str1 = "111", str2 = "1";

	cout << "lengthOfLastWord: " << s.addBinary(str1,str2) << endl;



	system("pause");
	return 0;
}